package com.etc.RCCPU001.controller;


import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.etc.RCCPU001.pojo.Userinfo;
import com.etc.RCCPU001.service.UserinfoService;
import com.etc.common.Code;
import com.etc.common.CodeMessage;

import sun.misc.BASE64Encoder;
	
/**
 * 
 * Filename:    UserController.java  
 * Description:   登录功能实现
 * Copyright:   Copyright (c) 2018-2019 All Rights Reserved.
 * Company:     chinasofti.com Inc.
 * @author:     qinxiaodong 
 * @version:    1.0  
 * Create at:   2020年3月3日 下午8:29:54  
 *  
 * Modification History:  
 * Date         Author      Version     Description  
 * ------------------------------------------------------------------  
 * 2020年3月3日      qinxiaodong      1.0         1.0 Version  
 *
 */
@RestController
public class UserController {
	

    @Value("${server.port}")
    String port;
    
    @Autowired
    UserinfoService userinfoServiceImpl;

    @RequestMapping("/hi")
    public String home(@RequestParam(value = "name", defaultValue = "a1") String name) {
        return "hi " + name + " ,i am from port:" + port;
    }
    
    /**
     * * 登录
     * @param uname 用户名
     * @param psd 密码
     * @return map 用户信息以及正确性
     */
    @RequestMapping("/login")
    public Object login(String uname,String psd) {
    	MessageDigest md5;
    	String newstr="";
		try {
			md5 = MessageDigest.getInstance("MD5");
			BASE64Encoder base64en = new BASE64Encoder();
	        //加密后的字符串
	       newstr=base64en.encode(md5.digest(psd.getBytes("utf-8")));
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
    	Userinfo user  = userinfoServiceImpl.login(uname,newstr);

    	Map<String,Object> map = new HashMap<String,Object>();
    	if (user == null) {
            map.put("user", user);
            map.put("flag", "fail");
            return map;
    	}
    	if (user.getId() != null) {
    		map.put("user", user);
    		map.put("flag", "success");
    	}else {
    		map.put("user", user);
    		map.put("flag", "fail");
    	}
    	return map;
    }
    /**
       * 注册用户
     * @param user 用户注册的所有信息
     * @return 注册是否成功
     */
    @RequestMapping("/register")
    public Object register(Userinfo user) {
    	//md5 密码加密
    	 //确定计算方法
        MessageDigest md5;
		try {
			md5 = MessageDigest.getInstance("MD5");
			BASE64Encoder base64en = new BASE64Encoder();
	        //加密后的字符串
	        String newstr=base64en.encode(md5.digest(user.getPsd().getBytes("utf-8")));
	        //	        设置新的密码
	        user.setPsd(newstr);
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
    	int flag =  userinfoServiceImpl.register(user);
    	
    	Map<String,Object> map = new HashMap<String,Object>();
    	if (flag > 0) {
    		map.put("flag", "success");
    	}else {
    		map.put("flag", "fail");
    	}
    	return map;
    	
    }
    
    
    @RequestMapping("/getBanner.action")
	public CodeMessage getBanner(Integer modid){
		List<Map> list = userinfoServiceImpl.getBanner(modid);
		return CodeMessage.success(Code.SUCESS,null, list); 
	} 
}
